package com.example.stuSysbackend.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.stuSysbackend.domain.ApprovalRecords;
import com.example.stuSysbackend.domain.Result;
import com.example.stuSysbackend.service.ApprovalRecordsService;
import com.example.stuSysbackend.mapper.ApprovalRecordsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @author Cool
 * @description 针对表【approval_records(审批表)】的数据库操作Service实现
 * @createDate 2024-04-16 16:54:55
 */
@Service
public class ApprovalRecordsServiceImpl extends ServiceImpl<ApprovalRecordsMapper, ApprovalRecords>
        implements ApprovalRecordsService{
    @Autowired
    private ApprovalRecordsMapper approvalRecordsMapper;

    private ApprovalRecords app;
    @Override
    public void insert() throws IOException {
        ApprovalRecords AR = saveApp(app);
        approvalRecordsMapper.insert(AR);
    }
    @Override
    public String saveFile(MultipartFile file) throws IOException {
        String fileName = file.getOriginalFilename();
        String basePath = "D:/work/xmut-stuSys-backend/uploads/";
        String fileExtension = getFileExtension(fileName);
        String uniqueFileName = UUID.randomUUID()+ "." + fileExtension;
        String filePath = basePath + "photo/" + uniqueFileName;
        File dest = new File(filePath);
        file.transferTo(dest);
        return filePath;
    }
    private String getFileExtension(String fileName) {
        if (fileName.lastIndexOf(".") != -1 && fileName.lastIndexOf(".") != 0) {
            return fileName.substring(fileName.lastIndexOf(".") + 1);
        } else {
            return "";
        }
    }
    @Override
    public ApprovalRecords saveApp(ApprovalRecords AP) {
        this.app = AP;
        LocalDateTime time = LocalDateTime.now();
        // 将 LocalDateTime 转换为 ZonedDateTime，使用系统默认时区
        ZonedDateTime zonedDateTime = time.atZone(ZoneId.systemDefault());
        // 将 ZonedDateTime 转换为 Instant
        Instant instant = zonedDateTime.toInstant();
        // 将 Instant 转换为 Date
        Date date = Date.from(instant);
        AP.setApproval_time(date);
        AP.setStatus(0);
        return AP;
    }

    @Override
    public List<ApprovalRecords> queryApproval(ApprovalRecords app) {
        LambdaQueryWrapper<ApprovalRecords> lqw = new LambdaQueryWrapper<>();
        if (app.getMatch_name() != null && !app.getMatch_name().isEmpty()) {
            lqw.like(ApprovalRecords::getMatch_name, app.getMatch_name());
        }
        if (app.getLevel() != null) {
            lqw.eq(ApprovalRecords::getLevel, app.getLevel());
        }
        if (app.getAward_time() != null && !app.getAward_time().isEmpty()) {
            lqw.eq(ApprovalRecords::getAward_time, app.getAward_time());
        }
        if (app.getStatus() != null) {
            lqw.eq(ApprovalRecords::getStatus, app.getStatus());
        }
        if (app.getApplicant() != null && !app.getApplicant().isEmpty()) {
            lqw.like(ApprovalRecords::getApplicant, app.getApplicant());
        }
        List<ApprovalRecords> list = approvalRecordsMapper.selectList(lqw);
        if (list == null || list.isEmpty()) {
            lqw.clear();
            list = approvalRecordsMapper.selectList(lqw);
        }
        return list;
    }

    @Override
    public void update(ApprovalRecords app) {
        approvalRecordsMapper.updateById(app);
    }

    @Override
    public void updateStatus1(Integer id) {
        approvalRecordsMapper.updateStatus1(id);
    }

    @Override
    public void updateStatus2(Integer id) {
        approvalRecordsMapper.updateStatus2(id);
    }

    @Override
    public void updateBack(Integer id) {
        approvalRecordsMapper.updateBack(id);
    }

    @Override
    public ApprovalRecords selectById(Integer id) {
        ApprovalRecords approvalRecords = approvalRecordsMapper.selectById(id);
        return approvalRecords;
    }

    @Override
    public void deleteById(Integer id) {
        approvalRecordsMapper.deleteById(id);
    }
}




